function sortFunc(array) {
    var left = 0;
    var right = array.length - 1;
    var base = array[0];
    if (array.length <= 1) {
        return array;
    } else if (array.length === 2) {
        if (left > right) {
            array[0] = right;
            array[1] = left;
        }
        return array;
    } else {
        function checkFinish() {
            return left === right;
        }

        function rightSearch() {
            while (right > left) {
                var temp = array[right];
                if (temp < base) {
                    array[left] = temp;
                    break;
                }
                right--;
            }
        }

        function leftSearch() {
            while (left < right) {
                var temp = array[left];
                if (temp > base) {
                    array[right] = temp;
                    break;
                }
                left++;
            }
        }
        while (!checkFinish()) {
            rightSearch();
            leftSearch();
        }
        array[left] = base;
        var leftArray = array.slice(0, left);
        var rightArray = array.slice(left + 1);
        leftArray = sortFunc(leftArray);
        rightArray = sortFunc(rightArray);

        leftArray.push(base);
        leftArray.concat(rightArray);
        return leftArray;
    }
}

var arr = [];
var util = require('util');

for (var i = 0; i < 100000; i++) {
    arr.push(Math.round(Math.random() * 1000) + 1);
}
console.time('sort');
arr = sortFunc(arr);
console.timeEnd('sort');

// var fs = require('fs');
// var stream = fs.createWriteStream("result.txt");
// stream.on('open', function(fd) {
//     arr.forEach(function(item) {
//         stream.write(item.toString() + '\r\n');
//     });
//     stream.end();
// });
